﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcs.Web.project
{
    public partial class listout : Web.Admin.BasePage
    {
        public string dogid = "NO";
        public string Decode(string str)
        {

            if (dogid == "NO")
            {
                if (Request.Cookies["dogid"] != null)
                    dogid = Request.Cookies["dogid"].Value.ToString();
            }
            return DecodeDog(str, dogid);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
                Page.DataBind();
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }
        protected void btnSearch1_Click(object sender, EventArgs e)
        {
            string projectname = lbl_projectname.Text;
            BindGV2(projectname);
        }


        public void BindData()
        {
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int projectId = (Convert.ToInt32(Request.Params["id"]));

                Maticsoft.BLL.project bll = new Maticsoft.BLL.project();
                Maticsoft.Model.project model = bll.GetModel(projectId);
                string projectName = model.projectName;
                lbl_projectname.Text = projectName;

                DataSet ds = new DataSet();
                StringBuilder strWhere = new StringBuilder();
                if (txtKeyword.Text.Trim() != "")
                {
                    if (Decode("1") == "1")
                        strWhere.AppendFormat(" and material like '%{0}%'", txtKeyword.Text.Trim());
                }
                strWhere.Append(string.Format("and target_project='{0}'", projectName));
                lcs.BLL.out_storageList obll = new BLL.out_storageList();
                ds = obll.GetListVn(strWhere.ToString());
                gridView.DataSource = ds;
                gridView.DataBind();
                BindGV2(projectName);
            }
        }
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            string projectname = lbl_projectname.Text;
            BindGV2(projectname);
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
                e.Row.Attributes.Add("onmouseover", "onmouseroverop(this)");
                //当鼠标离开的时候 将背景颜色恢复成之前的颜色
                e.Row.Attributes.Add("onmouseout", "onmouseroutop(this)");
                //单击行改变行背景颜色
                e.Row.Attributes.Add("onclick", "ItemOver(this)");
            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            int ID = (int)GridView2.DataKeys[e.RowIndex].Value;
            BLL.out_storage_detail bll = new BLL.out_storage_detail();
            bll.Delete(ID);
            string projectname = lbl_projectname.Text;
            BindGV2(projectname);
        }
        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
                e.Row.Attributes.Add("onmouseover", "onmouseroverop(this)");
                //当鼠标离开的时候 将背景颜色恢复成之前的颜色
                e.Row.Attributes.Add("onmouseout", "onmouseroutop(this)");
                //单击行改变行背景颜色
                e.Row.Attributes.Add("onclick", "ItemOver(this)");
            }
        }
        private void BindGV2(string projectname)
        {
            lcs.BLL.out_storage_detail odm = new BLL.out_storage_detail();
            StringBuilder strWhere = new StringBuilder();
            if (txtwl.Text.Trim() != "")
            {
               strWhere.AppendFormat(" material like '%{0}%'", txtwl.Text.Trim());
            }
            if(strWhere.ToString()!="")
            strWhere.Append(string.Format("and use_projectname='{0}'", projectname));
            else
                strWhere.Append(string.Format(" use_projectname='{0}'", projectname));
            DataSet ds = odm.GetListByVout_storage_detail_materialinfo(strWhere.ToString());

            this.GridView2.DataSource = ds;
            this.GridView2.DataBind();
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_out")
            {
                if (e.CommandArgument != null)
                {

                    int inid = Convert.ToInt32(e.CommandArgument.ToString());//库存ＩＤ
                    if (Session["login"] != null)
                    {
                        GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent));
                        TextBox txt_out_storage_count = drv.FindControl("txt_out_storage_count") as TextBox;//出库数量

                        DateTime out_storage_Time = DateTime.Now;
                        int mid = 0;
                        decimal out_count = 0;
                        decimal real_count = 0;
                        Label lbl_spid = drv.FindControl("lbl_spid") as Label;//mid
                        Label lbl_realCount = drv.FindControl("lbl_venNums") as Label;//数量
                        int.TryParse(lbl_spid.Text, out mid);
                        decimal.TryParse(lbl_realCount.Text, out real_count);
                        //未出库数量,从数据库中直接读取
                        //lcs.BLL.out_storage om = new BLL.out_storage();
                        //lcs.BLL.out_storageList olm = new BLL.out_storageList();
                        try
                        {
                            if (!string.IsNullOrEmpty(txt_out_storage_count.Text))
                            {
                                out_count = Convert.ToDecimal(txt_out_storage_count.Text);
                            }
                            else
                            {
                                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请填写分码数量！');</script>");
                            }

                        }
                        catch (Exception)
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('分码数量请填写数字！');</script>");
                            return;
                        }
                        if (real_count - out_count < 0)
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('分码数量大于出库数量！');</script>");
                            return;
                        }
                        #region 分码操作

                        lcs.BLL.out_storage_detail odm = new BLL.out_storage_detail();
                        List<lcs.Model.out_storage_detail> li_od = odm.GetModelList("and material_detail_id like '" + mid + "%' order by out_storage_detail_Id desc ");
                        int numstart = 0;
                        if (li_od.Count != 0)//如果没有分码
                        {
                            lcs.Model.out_storage_detail o = li_od[0];//最大值
                            string[] midcuts = o.material_detail_id.Split('_');
                            numstart = Convert.ToInt32(midcuts[1]);
                        }
                        for (int i = 0; i < out_count; i++)
                        {
                            lcs.Model.out_storage_detail o = new Model.out_storage_detail();
                            o.material_id = mid;
                            o.out_storage_id = inid;//出库清单表id
                            o.use_projectname = lbl_projectname.Text;
                            o.nums = 1;
                            o.op_uid = -1;//设置巡检检查人为-1,在分发巡检人时更改
                            o.material_detail_id = mid + "_" + (numstart+i + 1);//分码
                            odm.Add(o);
                        }
                        BindGV2(lbl_projectname.Text);
                        BindData();
                        #endregion
                    }
                    else
                    {
                        Response.Redirect("../Default.aspx");
                    }


                }
            }
        }
    }
}